﻿// functions needed on root source file
//
// showGoLogin():
// shows restartprompt telling user to login first
//
// nextframe():
// enter program, attract -> program
//
// restartProgram():
// restarts the program, going to attract
//
// changeUser():
// shows restartprompt when different user swipes felica
// 
// closePrompt():
// hides restartpromt. certain things might need to be done depending on kiosk
package com.unifiedfield{
	import com.unifiedfield.cHoster;
	import flash.utils.Timer;
	import flash.display.MovieClip;
	public class FelicaControl {
		var cHost:cHoster;
		var hHostStartTimer:Timer;
		var hCameraStartTimer:Timer;
		var hFelicaStartTimer:Timer;
		var hFelicaPollingTimer:Timer;
		var mainstage:MovieClip;
		var currentID:String;
		var askrestart:MovieClip;
		var kiosk:String;
		var lastID:String;
		var lasttime:Number;
		public function FelicaControl(main,restartprompt,kioskname="") {
			cHost=new cHoster  ;
			mainstage=main;
			askrestart=restartprompt;
			lastID=currentID="0";
			lasttime=0;
			askrestart.visible=false;
			askrestart.setfunc(restartmain,cancelrestart);
			kiosk=kioskname;
		}
		public function beginFelica() {
			hHostStartTimer=new Timer(1000,0);
			hHostStartTimer.addEventListener("timer",Timer_HostStart);
			hHostStartTimer.start();
		}
		public function waitForNewUser() {
			currentID="0";
			if (! hFelicaPollingTimer.running) {
				hFelicaPollingTimer.start();
			}
		}
		public function setID(n) {
			currentID=n;
		}
		public function getID():String {
			return currentID;
		}
		public function endPoll() {
			hFelicaPollingTimer.stop();
		}
		private function Timer_HostStart(ev) {
			trace("Timer_HostStart");
			hHostStartTimer.reset();
			hHostStartTimer.removeEventListener("timer",Timer_HostStart);
			FelicaOpen();
		}
		private function FelicaOpen() {
			cHost.FelicaOpen();
			hFelicaStartTimer=new Timer(1000,0);
			hFelicaStartTimer.addEventListener("timer",Timer_FelicaStart);
			hFelicaStartTimer.start();
		}
		private function Timer_FelicaStart(ev) {
			//cHost.AddToLog("Timer_FelicaStart");
			hFelicaStartTimer.reset();
			hFelicaStartTimer.removeEventListener("timer",Timer_FelicaStart);
			mainstage.gotoAndPlay("init");
			startPolling();
		}
		private function startPolling() {
			cHost.FelicaStartPolling();
			//cHost.AddToLog("startPolling");
			hFelicaPollingTimer=new Timer(10,0);
			hFelicaPollingTimer.addEventListener("timer",Timer_CardCheck);
			hFelicaPollingTimer.start();
		}
		private function Timer_CardCheck(ev) {
			var read=cHost.FelicaCheckForCard();
			if (read != "0") {
				var dat=new Date();
				if (kiosk == "login" && lastID == read && dat-lasttime<300000) {
					cHost.AddToLog("RESWIPE ERROR PREVENTION");
				} else if (currentID == "0") {// SWIPE IN
					var logged=cHost.StationActivateGuest(read);
					if (logged == "-1" || logged == "0" && kiosk != "login") {// not logged in
						mainstage.showGoLogin();
					} else if (logged != "null") {
						lastID=currentID=read;
						mainstage.nextframe();
					}
				} else if (currentID != "0" && read != currentID && read != "null") {// INTERRUPT
					if (kiosk == "Animation") {
						cHost.Animation_StopRefresh();
					}
					mainstage.changeUser();
				}
			}
		}
		private function cancelrestart(e) {
			askrestart.visible=false;
			mainstage.closePrompt();
		}
		private function restartmain(e) {
			askrestart.visible=false;
			mainstage.restartProgram();
			if (kiosk == "Animation") {
				mainstage.changemask(0);
				cHost.Animation_StartRefresh();
			}
		}
	}
}